package com.songqintuan.platform.foundation;

import java.io.Serializable;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

import net.sf.cglib.beans.BeanCopier;

import com.songqintuan.common.util.MessageFactory;
import com.songqintuan.common.util.SpringFacesUtil;
import com.songqintuan.model.po.Company;
import com.songqintuan.model.vo.CompanyVo;
import com.songqintuan.persist.service.hibernate.ICompanyService;
import com.songqintuan.platform.base.IManagePojoPageBean;
import com.songqintuan.platform.base.ManagePojoPageBean;
import com.songqintuan.platform.common.constant.PlatformConstants;
import com.songqintuan.platform.common.constant.PlatformSpringBeanConstants;
import com.songqintuan.platform.page.CompanyPage;



@ManagedBean(name="manageCompanyPage")
@SessionScoped
public class ManageCompanyPageBean extends ManagePojoPageBean implements IManagePojoPageBean, Serializable {

	private static final long serialVersionUID = 5054087821033164829L;
	
	private CompanyPage page;
	
	private CompanyVo view;
	private CompanyVo edit;

	
	public CompanyPage getPage() {
		return page;
	}

	public void setPage(CompanyPage page) {
		this.page = page;
	}

	public CompanyVo getView() {
		return view;
	}

	public void setView(CompanyVo view) {
		this.view = view;
	}

	public CompanyVo getEdit() {
		return edit;
	}

	public void setEdit(CompanyVo edit) {
		this.edit = edit;
	}

	public ManageCompanyPageBean(){
		
		Company ob = new Company();
		page = new CompanyPage(1,20,ob);
		page.getPageDatas();
	}

	public String selectPageNo() {
		String result = null;
		String pageNo = (String) SpringFacesUtil.getRequestParameter("pageNo");
		if (null != this.getPage().getPageNo()) {
			if (this.getPage().getPageNo().intValue() == Integer.valueOf(pageNo).intValue()) {
				return result;
			}
		}
		this.getPage().setPageNo(Integer.valueOf(pageNo).intValue());
		this.getPage().getPageDatas();
		return result;
		
	}
	public String viewAction(){
		String result = null;
		clearErrorMessages();
		ICompanyService<Company,String> companyService = (ICompanyService<Company,String>)SpringFacesUtil.getSpringBean(PlatformSpringBeanConstants.COMPANY_SERVICE_BEAN_NAME);
		companyService.setEntityClass(Company.class);
		companyService.setPKClass(String.class);
		String companyId= (String) SpringFacesUtil.getRequestParameter("companyId");	
		Company company = companyService.findById(companyId);
		CompanyVo companyVo =new CompanyVo();
		BeanCopier copyHere = BeanCopier.create(Company.class, CompanyVo.class, false);
		copyHere.copy(company, companyVo, null);
		this.setAction(PlatformConstants.ACTION_VIEW_TYPE);
		this.setView(companyVo);
		return result;
	}
	public String editAction(){//get data
		String result = null;
		clearErrorMessages();
		ICompanyService<Company,String> companyService = (ICompanyService<Company,String>)SpringFacesUtil.getSpringBean(PlatformSpringBeanConstants.COMPANY_SERVICE_BEAN_NAME);
		companyService.setEntityClass(Company.class);
		companyService.setPKClass(String.class);
		String companyId= (String) SpringFacesUtil.getRequestParameter("companyId");
		Company company = companyService.findById(companyId);
		CompanyVo CompanyVo =new CompanyVo();
		BeanCopier copyHere = BeanCopier.create(Company.class, CompanyVo.class, false);
		copyHere.copy(company, CompanyVo, null);
		this.setAction(PlatformConstants.ACTION_EDIT_TYPE);
		this.setEdit(CompanyVo);
		return result;
	}
	public String viewToEditAction() {
		String result = null;
		clearErrorMessages();
		this.setEdit(this.getView());
		this.setView(null);
		this.setAction(PlatformConstants.ACTION_EDIT_TYPE);
		return result;
	}
	public String updateAction(){
		String result = null;
		try {
			this.clearErrorMessages();
			CompanyValidator(this.getErrorMessages(),this.getEdit());
			if (!(this.getErrorMessages().isEmpty())) {
				return null;
			}
			ICompanyService<Company,String> companyService = (ICompanyService<Company,String>)SpringFacesUtil.getSpringBean(PlatformSpringBeanConstants.COMPANY_SERVICE_BEAN_NAME);
			companyService.setEntityClass(Company.class);
			companyService.setPKClass(String.class);
			Company company = companyService.findById(this.getEdit().getId());
			company.setName(this.getEdit().getName());
			company.setLabel(this.getEdit().getLabel());
			company.setAddress(this.getEdit().getAddress());
			company.setZip(this.getEdit().getZip());
			company.setContactPersonName(this.getEdit().getContactPersonName());
			company.setMobile(this.getEdit().getMobile());
			company.setPhone(this.getEdit().getPhone());
			company.setFax(this.getEdit().getFax());
			company.setEmail(this.getEdit().getEmail());
			company.setUrl(this.getEdit().getUrl());
			company.setDescription(this.getEdit().getDescription());
			company.setRemarks(this.getEdit().getRemarks());
			companyService.saveOrUpdate(company);
		}catch (Exception e) {
			String updateErrorN18 = MessageFactory.getMessage(PlatformConstants.USER_BUNDLE_NAME,"update.data.version.not.same.error.message", FacesMessage.SEVERITY_ERROR).getDetail();
			this.setEditDataInPageFlag(false);
			this.getErrorMessages().add(updateErrorN18);
			return result;
		} finally {
            //do nothing;//关闭session的时候还会报，并且在session那里抓取了所有的Exception。
        }
		return result;
	}
	public String preCreateAction(){
		String result = null;
		clearErrorMessages();
		this.setAction(PlatformConstants.ACTION_NEW_TYPE);
		CompanyVo CompanyVo =new CompanyVo();
		this.setEdit(CompanyVo);
		return result;
	}
	
	public String saveAction(){//create new
		String result = null;
		clearErrorMessages();
		CompanyValidator(this.getErrorMessages(),this.getEdit());
		if (!(this.getErrorMessages().isEmpty())) {
			return null;
		}
		ICompanyService<Company,String> CompanyService = (ICompanyService<Company,String>)SpringFacesUtil.getSpringBean(PlatformSpringBeanConstants.COMPANY_SERVICE_BEAN_NAME);
		CompanyService.setEntityClass(Company.class);
		CompanyService.setPKClass(String.class);
		Company company = CompanyService.findById(String.valueOf(1));//总公司id=1
		if (null == company) {
			String companyNotExistDisplayN18 = MessageFactory.getMessage(PlatformConstants.USER_BUNDLE_NAME,"head.company.notexist.display", FacesMessage.SEVERITY_ERROR).getDetail();
			String companyNotExistErrorN18 = MessageFactory.getMessage(PlatformConstants.USER_BUNDLE_NAME,"data.notexist.error.message", FacesMessage.SEVERITY_ERROR,companyNotExistDisplayN18).getDetail();
			this.getErrorMessages().add(companyNotExistErrorN18);
		}
		if (!(this.getErrorMessages().isEmpty())) {
			return null;
		}
		Company newCompany = new Company();
		BeanCopier copyHere = BeanCopier.create(CompanyVo.class, Company.class, false);
		copyHere.copy(this.getEdit(), newCompany, null);
		CompanyService.save(newCompany);
		return result;
	}
	public String refreshPageAction(){
		String result = null;
		this.getPage().getPageDatas();
		return result;
	}
	public String searchPageAction(){
		return null;
	}
	private void CompanyValidator(List<String> errorMessages,CompanyVo eidt){
		/*String CompanySerialNumberN18 = MessageFactory.getMessage(EjdConstants.USER_BUNDLE_NAME,"object.serialNumber.display", FacesMessage.SEVERITY_ERROR).getDetail();
		String CompanyCodeDisplayN18 = MessageFactory.getMessage(EjdConstants.USER_BUNDLE_NAME,"Company.code.display", FacesMessage.SEVERITY_ERROR).getDetail();
		String CompanyNameDisplayN18 = MessageFactory.getMessage(EjdConstants.USER_BUNDLE_NAME,"Company.name.display", FacesMessage.SEVERITY_ERROR).getDetail();
		ValidatorUtil.IntegerValidator(errorMessages, (null != edit.getSerialNumber())?edit.getSerialNumber().toString():"", CompanySerialNumberN18);
		ValidatorUtil.EmptyValidatorForInput(errorMessages, edit.getCode(), CompanyCodeDisplayN18);
		ValidatorUtil.WhitespaceValidator(errorMessages, edit.getCode(), CompanyCodeDisplayN18);
		ValidatorUtil.WhitespaceValidator(errorMessages, edit.getName(), CompanyNameDisplayN18);*/
	}
}
